package com.qf.usermybatis.controller;

import com.github.pagehelper.PageInfo;
import com.qf.usermybatis.entity.User;
import com.qf.usermybatis.service.UserService;
import com.qf.usermybatis.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

@WebServlet("/users")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.setContentType("text/html;charset=utf-8");
        try {
            String pageNum = request.getParameter("pageNum");
            String pageSize = request.getParameter("pageSize");
            if (Objects.isNull(pageNum) || "".equals(pageNum)) {
                pageNum = "1";
            }
            if (Objects.isNull(pageSize) || "".equals(pageSize)) {
                pageSize = "5";
            }

            PageInfo<User> pageInfo = userService.selectAll(Integer.valueOf(pageNum), Integer.valueOf(pageSize));

            request.setAttribute("pageInfo", pageInfo);

            request.getRequestDispatcher("index.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}